package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;
	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		List<JSONObject> categories1= new ArrayList<>();
		//一次性查询出所有的三级分类
		List<BaseCategoryView> baseCategoryViews = this.baseCategoryViewMapper.selectList(null);
		if (CollectionUtils.isEmpty(baseCategoryViews)){
			return null;
		}
		//根据一级分类id进行分组，获取一级分类数据Map<一级分类id,同一个一级分类ID下的数据集合>
		Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		category1Map.forEach((category1Id, category2List) -> {
			//每一个kv转化成一级分类的json对象
			JSONObject jsonObject1 = new JSONObject();
			//把一级分类json对象放入分类集合
			categories1.add(jsonObject1);
			//设置一级分类id
			jsonObject1.put("categoryId", category1Id);
			//设置一级分类name,由于categorylist是根据具体数据进行分组的，所有该分组数据不可能为空，否则不会有改组数据
			jsonObject1.put("categoryName",category2List.get(0).getCategory1Name());
			//设置一级分类下的二级分类
			List<JSONObject> categories2= new ArrayList<>();
			jsonObject1.put("categoryChild",categories2);
			//根据二级分类id对同一个一级分类下的数据进行分组，获取二级分类数据Map<二级分类id,同一个二级分类ID下的数据集合>
			Map<Long, List<BaseCategoryView>> category2Map = category2List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			category2Map.forEach((category2Id, category3List) -> {
				//把每一个kv转化成二级分类的json对象
				JSONObject jsonObject2 = new JSONObject();
				categories2.add(jsonObject2);
				jsonObject2.put("categoryId", category2Id);
				jsonObject2.put("categoryName",category3List.get(0).getCategory2Name());
				//设置二级分类的三级分类
				//循环遍历二级分类下的数据集合，转化成三级分类json对象集合
				jsonObject2.put("categoryChild",category3List.stream().map(baseCategoryView -> {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					return jsonObject3;
				}).collect(Collectors.toList()));
			});
		});
		return categories1;
	}

	@Override
	public List<BaseAttribute>  findAttributeByCategory1Id(Long category1Id) {
		//调用mapper层方法
		return baseAttributeMapper.selectBaseAttributeList(category1Id);
	}
}
